package br.com.empresa.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import br.com.empresa.dao.UsuarioDAO;
import br.com.empresa.modelo.Usuario;

public class ListagemUsuarioGUI extends JFrame {

	private JPanel contentPane;
	private JTable tabela;
	private DefaultTableModel modelo = new DefaultTableModel();

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginUsuarioGUI frame = new LoginUsuarioGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ListagemUsuarioGUI() {
		criarTabela();
		setTitle("Betha Sistemas");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 607, 412);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(0, 0, 591, 374);
		contentPane.add(panel);
		panel.setLayout(null);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(Color.DARK_GRAY);
		panel_1.setBounds(0, 0, 718, 46);
		panel.add(panel_1);
		panel_1.setLayout(null);

		JLabel lblNewLabel = new JLabel("USU\u00C1RIOS");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(10, 11, 510, 20);
		lblNewLabel.setForeground(Color.WHITE);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
		panel_1.add(lblNewLabel);

		JButton btNovo = new JButton("Inserir");
		btNovo.setIcon(null);
		btNovo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CadastroUsuarioGUI gui = new CadastroUsuarioGUI();
				gui.setVisible(true);
				dispose();
			}
		});
		btNovo.setBounds(25, 64, 89, 23);
		panel.add(btNovo);

		JLabel lblDesenvolvidoPorRosiclia = new JLabel(
				"Desenvolvido por Rosicl\u00E9ia Frasson");
		lblDesenvolvidoPorRosiclia.setBounds(0, 436, 530, 14);
		panel.add(lblDesenvolvidoPorRosiclia);
		lblDesenvolvidoPorRosiclia.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDesenvolvidoPorRosiclia.setForeground(Color.WHITE);

		JPanel panel_2 = new JPanel();
		panel_2.setBounds(0, 429, 718, 21);
		panel.add(panel_2);
		panel_2.setBackground(Color.DARK_GRAY);
		panel_2.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 135, 571, 228);
		scrollPane.setViewportView(tabela);
		panel.add(scrollPane);

		JButton btnVoltar = new JButton("Menu");
		btnVoltar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				MenuSistemaGUI gui = new MenuSistemaGUI();
				gui.setVisible(true);
			}
		});
		btnVoltar.setBounds(492, 64, 89, 23);
		panel.add(btnVoltar);
	}

	private void criarTabela() {
		tabela = new JTable(modelo);
		modelo.addColumn("Login");
		modelo.addColumn("Senha");
		modelo.addColumn("Nome");
		modelo.addColumn("Status");
		String Status = "";

		UsuarioDAO dao = new UsuarioDAO();
		for (Usuario userObj : dao.listar()) {
			if (userObj.getInativoUsuario().equals("1")) {
				Status = "Inativo";
			} else {
				Status = "Ativo";
			}
			modelo.addRow(new Object[] { userObj.getLoginUsuario(),
					"**********", userObj.getNomeUsuario(), Status });
		}
	}
}
